﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Collections.ObjectModel;
using DTC.Database;
using DTC.Popups;

namespace DTC.Pages
{
    /// <summary>
    /// Interaction logic for JournalPage.xaml
    /// </summary>
    public partial class JournalPage : Page
    {

        private DateTime MiddleDate;

        public JournalPage()
        {
            InitializeComponent();
            MiddleDate = DateTime.Today;
            MoveLeftButton.Content = MiddleDate.AddDays(-1).ToShortDateString();
            DateTB.Text = MiddleDate.ToShortDateString();
            MoveRightButton.Content = MiddleDate.AddDays(1).ToShortDateString();
            doctors = new ObservableCollection<DTCDoctor>(App.DBI.GetAllDoctors());
            DoctorsListBox.ItemsSource = doctors;
            Chart.Doctors = doctors;
            SetVisits();
        }

        private ObservableCollection<DTCDoctor> doctors;
        private ObservableCollection<DTCVisit> visits;
        private void SetVisits()
        {
            visits = new ObservableCollection<DTCVisit>(App.DBI.GetVisitsByDate(MiddleDate));
            Chart.Visits = visits;
        }

        private void MoveLeftButton_Click(object sender, RoutedEventArgs e)
        {
            MiddleDate = MiddleDate.AddDays(-1);
            MoveLeftButton.Content = MiddleDate.AddDays(-1).ToShortDateString();
            DateTB.Text = MiddleDate.ToShortDateString();
            MoveRightButton.Content = MiddleDate.AddDays(1).ToShortDateString();
            Chart.Date = MiddleDate;
            SetVisits();
        }

        private void MoveRightButton_Click(object sender, RoutedEventArgs e)
        {
            MiddleDate = MiddleDate.AddDays(1);
            MoveLeftButton.Content = MiddleDate.AddDays(-1).ToShortDateString();
            DateTB.Text = MiddleDate.ToShortDateString();
            MoveRightButton.Content = MiddleDate.AddDays(1).ToShortDateString();
            Chart.Date = MiddleDate;
            SetVisits();
        }

        private void VisitsListBox_SizeChanged(object sender, SizeChangedEventArgs e)
        {
        }

        private void VisitsListBox_MouseDoubleClick(object sender, MouseButtonEventArgs e)
        {
        }

        private void calendar1_SelectedDatesChanged(object sender, SelectionChangedEventArgs e)
        {
            MiddleDate = (DateTime) calendar1.SelectedDate;
            DateTB.Text = MiddleDate.ToShortDateString();
            Chart.Date = MiddleDate;
            SetVisits();
        }

        private void NewDoctorButton_Click(object sender, RoutedEventArgs e)
        {
            DoctorPopup dp = new DoctorPopup();
            dp.Owner = App.Current.MainWindow;
            if (dp.ShowDialog()==true)
            {
                doctors.Add(dp.Doctor);
            }
        }

        private void ViewDoctorButton_Click(object sender, RoutedEventArgs e)
        {
            Button cmd = (Button)sender;
            DoctorPopup dp = new DoctorPopup();
            dp.Doctor = (DTCDoctor)cmd.Tag;
            dp.ShowDialog();
        }

        private void DeleteDoctorButton_Click(object sender, RoutedEventArgs e)
        {
            Button cmd = (Button)sender;
            DTCDoctor doc = (DTCDoctor)cmd.Tag;
            App.DBI.Db.DTCDoctors.DeleteOnSubmit(doc);
            App.DBI.Db.SubmitChanges();
        }
    }       
}
